package com.example.service;

import com.example.entity.user.Farmer;
import com.example.entity.item.Items;
import com.example.entity.user.Room;
import com.example.entity.dto.FarmerDTO;
import com.example.mapper.FarmerMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

//农民业务处理
@Service
public class FarmerService {

    @Resource
    private FarmerMapper farmerMapper;

    public void deleteFarmerById(Integer id){
        farmerMapper.deleteFarmerById(id);}

    public void updateFarmerById(Farmer farmer){farmerMapper.updateFarmerById(farmer);}

    public Farmer selectFarmerById(Integer id){return farmerMapper.selectFarmerById(id);}

    public FarmerDTO selectFarmerByUsername(String username){return farmerMapper.selectFarmerByUsernameDTO(username);}

    public List<Farmer> selectFarmerAll(Farmer farmer){return farmerMapper.selectFarmerAll(farmer);}

    public PageInfo<Farmer> selectFarmerPage(Farmer farmer, Integer pageNum, Integer pageSize)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<Farmer> list=farmerMapper.selectFarmerAll(farmer);
        return PageInfo.of(list);
    }

    public PageInfo<Items> selectItemPage(String itemOwner,Integer pageNum,Integer pageSize)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<Items> list=farmerMapper.selectItemByItemOwner(itemOwner);
        return PageInfo.of(list);
    }

    public Room selectRoomById(String roomId){return farmerMapper.selectRoomById(roomId);}

    public String selectNewItemId(String itemCategory)
    {
        String info="";
        if(itemCategory.equals("sg"))
        {
            info=farmerMapper.selectNewItemId("F");
        }
        else if(itemCategory.equals("nfjg"))
        {
            info=farmerMapper.selectNewItemId("D");
        }
        else if(itemCategory.equals("qcrd"))
        {
            info=farmerMapper.selectNewItemId("M");
        }
        else if(itemCategory.equals("nmgj"))
        {
            info=farmerMapper.selectNewItemId("P");
        }
        else if(itemCategory.equals("lymm"))
        {
            info=farmerMapper.selectNewItemId("R");
        }
        else if(itemCategory.equals("zzzm"))
        {
            info=farmerMapper.selectNewItemId("S");
        }
        else if(itemCategory.equals("mmhc"))
        {
            info=farmerMapper.selectNewItemId("T");
        }
        else if(itemCategory.equals("sc"))
        {
            info=farmerMapper.selectNewItemId("V");
        }
        else if(itemCategory.equals("shuic"))
        {
            info=farmerMapper.selectNewItemId("W");
        }
        else if(itemCategory.equals("zyc"))
        {
            info=farmerMapper.selectNewItemId("Z");
        }

        String letterPart=info.substring(0,1);
        int numberPart=Integer.parseInt(info.substring(1))+1;
        String newNumberPart=String.format("%03d",numberPart);
        String newInfo=letterPart+newNumberPart;
        return newInfo;
    }

    public void JoinItem(String ItemId,String itemName,String itemPrice,String itemStock,String itemDesc)
    {
        double price=Double.parseDouble(itemPrice);
        int stock=Integer.parseInt(itemStock);
        farmerMapper.JoinItem(ItemId,itemName,price,stock,itemDesc);
    }

    public void JoinItemList(String ItemId,String itemName,String itemCategory,String itemType,String itemClass)
    {
        farmerMapper.JoinItemList(ItemId,itemName,itemCategory,itemType,itemClass);
    }

}
